-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve time precision on decode #676
Conversation
Thank you for the PR. I honestly don't know how we should proceed. It's a real shame that pg does not send the precision over the wire. I created a little test rig below: Mix.install([
{:ecto_sql, "~> 3.10"},
{:postgrex, ">= 0.0.0", github: "elixir-ecto/postgrex", override: true}
# {:postgrex, ">= 0.0.0", github: "thenrio/postgrex", branch: "time", override: true}
])
Application.put_env(:foo, Repo, database: "mix_install_examples")
defmodule Repo do
use Ecto.Repo,
adapter: Ecto.Adapters.Postgres,
otp_app: :foo
end
defmodule Main do
import Ecto.Query, warn: false
def main do
{:ok, _} = Supervisor.start_link([Repo], strategy: :one_for_one)
Repo.checkout(fn ->
Repo.query!("CREATE TEMPORARY TABLE times (time time(6))")
Repo.query!("INSERT INTO times VALUES ('00:00:00'), ('00:00:00.000'), ('00:00:00.123')")
IO.inspect(Repo.query!("SELECT * FROM times").rows)
end)
end
end
Main.main() and we have:
I don't have a great mental model for this but I feel like if we standardise on microseconds precision like today at least it is consistent. |
Maybe it doesn't matter, e.g. Ecto distinguishes between :time and :time_usec types and upscales/downscales accordingly anyway. |
If it is impossible for us to know, then I would prefer to keep the current behaviour. This is because someone may actually store |
postgres lets you specify the precision in the type. something like |
Looking at Postgrex.Extensions.Time there is no indicator on the wire so we can’t do much and need to settle one way or another. |
I think @wojtekmach is correct. The extension is generic for any time precision. We don't have the information for a given time. :( |
Indeed, you are right @wojtekmach. May be we can improve documentation, with examples? For the record, I ran the postgres jdbc driver, with log turned on, for both execute of the prepared statements: "select now()::time" Both yield the same logging
In both case, the "type" of first column is the same "Field(now,TIME,8,T) "... |
There is an indicator in the row description message though. If I print out the query("CREATE TABLE test (micro timestamp, milli timestamp(3))", [])
query("SELECT * FROM test", []) {"micro", -1}
{"milli", 3} This is probably how other people would know the correct precision. So what I mean is if we can somehow change the code to utilize it that would be the "correct" way. |
Indeed, throwing a dgb in the code shows that (far better than my java experiment!).
We have a |
A go and see shows that we we do not keep the
From So a plan of changes could be:
postgrex/lib/postgrex/protocol.ex Line 2259 in ef6bd2f
postgrex/lib/postgrex/protocol.ex Line 3285 in ef6bd2f
Now I do not understand postgrex/lib/postgrex/type_module.ex Line 947 in ef6bd2f
|
Fixes #675.